package com.gdust.lease.web.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gdust.lease.model.entity.LeaseAgreement;
import com.gdust.lease.model.enums.LeaseStatus;
import com.gdust.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * ClassName: ScheduledTasks
 * Package: com.gdust.lease.web.admin.schedule
 * Description:定时任务类，用于处理与租赁协议相关的定时任务。
 *
 * @Author syun
 * @Create 2025/1/7 22:52
 * @Version 1.0
 */
@Component
public class ScheduledTasks {
    //注入租约信息表的service
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    /**
     * 定时任务：每天凌晨执行，检查租赁协议状态。
     *
     * 0 0 0 * * *:每天的凌晨0点触发。
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        Date now = new Date();
        //租赁结束日期小于等于当前时间的租赁协议
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
        //租赁状态为已签署（SIGNED）的租赁协议
        updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        //状态为已签署（SIGNED）或正在退租（WITHDRAWING）的租赁协议
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);

        //调用 MyBatis-Plus 提供的封装方法，更新操作
        leaseAgreementService.update(updateWrapper);
    }
}
